
package com.tianyang.yukuaifu.utils;

import android.annotation.TargetApi;
import android.os.Build;
import android.util.Log;
import android.util.LruCache;

import com.tianyang.yukuaifu.base.CommonProvider;
import com.tianyang.yukuaifu.utils.annotation.ModuleAnnotation;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Description :公共Provider，对外提供Provider，实现者先在静态代码块中注册，调用者调用getProvider获取 Created
 */
public class PublicProviderUtil {

    private static final int CACHE_SIZE = 10;

    protected static List<Class> providers = new ArrayList<>();

    private static LruCache<String, Object> mCache = new LruCache<>(CACHE_SIZE);

    /**
     * 注册provider，在静态代码块中加入到List中，注意，Provider构造不能私有
     */
    static {
        /* 公共 */
        providers.add(CommonProvider.class);
    }

    /**
     * 是不是继承了次接口
     *
     * @param inter 接口类
     * @param imp 实现类
     * @return 返回关系
     */
    private static boolean isExtendsInterface(Class inter, Class imp) {

        Class[] cls = imp.getInterfaces();
        for (Class aClass : cls) {
            if (aClass.getName().equals(inter.getName())) {
                return true;
            }
        }
        return false;

    }

    /**
     * 调用此方法获取公共Provider的实现
     * 例如：获取RecenCoversationProviderImp，调用：PublicProvider.getProvider(
     * RecenCoversationProvider.class);
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    @SuppressWarnings("unchecked")
    public static <T> T getProvider(Class<T> c) {
        Object providerImpl = mCache.get(c.getSimpleName());
        try {
            if (null != providerImpl) {
                return (T)mCache.get(c.getSimpleName());
            }
            for (Class mClass : providers) {
                if (isExtendsInterface(c, mClass)) {
                    providerImpl = mClass.newInstance();
                    mCache.put(c.getSimpleName(), providerImpl);
                    break;
                }
            }
            if (providerImpl == null) {
                Log.d(PublicProviderUtil.class.getName(), "Provider没有找到");
                return null;
            }
        } catch (InstantiationException | IllegalAccessException e) {
            Log.e(PublicProviderUtil.class.getName(), "Provider初始化异常");
            e.printStackTrace();
        }
        return (T)providerImpl;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static List<Object> getProviderWithModuleAnnotation(String module) {
        List<Object> list = new ArrayList<>();
        for (Class mClass : providers) {
            if (mClass.getAnnotation(ModuleAnnotation.class) != null) {
                ModuleAnnotation moduleAnnotation = (ModuleAnnotation)mClass
                        .getAnnotation(ModuleAnnotation.class);
                if (moduleAnnotation.module().toLowerCase(Locale.US)
                        .contains(module.toLowerCase(Locale.US))) {
                    try {
                        list.add(mClass.newInstance());
                    } catch (InstantiationException | IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
        return list;
    }
}
